package com.googlecode.sc2dm.receiver;

import android.content.Intent;
import com.googlecode.sc2dm.log.Logger;
import com.googlecode.sc2dm.register.util.*;
import junit.framework.TestCase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author Jarle Hansen (jarle@jarlehansen.net)
 * Created: 7:52 PM - 9/18/11
 */
public class OnMessageTest extends TestCase {
    private MessageReceiver messageReceiver;
    private Intent intent;
    private List<Class<?>> annotatedClasses;


    @Override
    protected void setUp() {
        messageReceiver = new MessageReceiver();
        intent = new Intent();
        MessageStorageUtil.MSG.reset();

        annotatedClasses = new ArrayList<Class<?>>();
    }


    public void testOnRegisteredTwoArgs() {
        annotatedClasses.add(AnnotatedTwoArgs.class);

        intent.putExtra("msg", "TEST-ACTION");
        messageReceiver.findAnnotations(annotatedClasses);
        messageReceiver.onMessage(null, intent);

        assertEquals("TEST-ACTION", MessageStorageUtil.MSG.getIdentifier());
    }

    public void testOnRegisteredOneArgContext() {
        annotatedClasses.add(AnnotatedOneArg1.class);

        messageReceiver.findAnnotations(annotatedClasses);
        messageReceiver.onMessage(null, intent);
    }

    public void testOnRegisteredOneArgRegId() {
        annotatedClasses.add(AnnotatedOneArg2.class);

        intent.putExtra("msg", "TEST-ACTION2");
        messageReceiver.findAnnotations(annotatedClasses);
        messageReceiver.onMessage(null, intent);

        assertEquals("TEST-ACTION2", MessageStorageUtil.MSG.getIdentifier());
    }

    public void testNotAnnotated() {
        annotatedClasses.add(NotAnnotated.class);

        messageReceiver.findAnnotations(annotatedClasses);
        messageReceiver.onMessage(null, intent);
    }

    public void testPerformance() {
        annotatedClasses.add(AnnotatedTwoArgs.class);

        messageReceiver.findAnnotations(annotatedClasses);

        long start = System.currentTimeMillis();
        messageReceiver.onMessage(null, intent);
        long end = System.currentTimeMillis();

        Logger.i("Time used: " + (end - start));
    }
}
